<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace app\Libs\Zip;

use ZipArchive;

class Zip
{
    private $zip;

    private $ignoredNames;

    public function __construct()
    {
        $this->zip = new ZipArchive();
    }


    /**
     * 添加文件到压缩包ZIP
     *
     * @param string $file
     * @param array  $folder
     * @param        $ignored
     *
     * @return bool
     */
    public function createZip( string $file, array $folder, array $ignored = NULL )
    {
        $this->ignoredNames = is_array($ignored) ? $ignored : $ignored ? [ $ignored ] : [];
        if ( ! $this->zip->open((string) $file, \ZipArchive::CREATE) ) return FALSE;
        foreach ( (array) $folder as $value ) if ( ! in_array($value, $this->ignoredNames) ) if ( ! $this->zip->addFile($value, pathinfo($value, PATHINFO_BASENAME)) ) return FALSE;
        $this->zip->close();
        return TRUE;
    }
}
